package com.pagepub.expo.action.corp;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.pagepub.comm.util.FileUploadUtil;
import com.pagepub.expo.entity.CorpCommpany;
import com.pagepub.expo.entity.IdName;
import com.pagepub.expo.manager.AppSeqMng;
import com.pagepub.expo.manager.CorpCommpanyMng;
import com.pagepub.expo.manager.CorpTemplateMng;
@Controller
public class CorpCommpanyAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	@Qualifier("corpCommpanyMng")
	private CorpCommpanyMng corpCommpanyMng;
	
	@Autowired
	@Qualifier("appSeqMng")
	private AppSeqMng appSeqMng;
	
	@Autowired
	@Qualifier("corpTemplateMng")
	private CorpTemplateMng corpTemplateMng;

	public List<CorpCommpany> rows = new ArrayList<CorpCommpany>();
	
	private boolean success;
	public int start = 0;
	public int limit = 0;
	public long newId = 0;
	
	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	
	public long getNewId() {
		return newId;
	}

	public void setNewId(long newId) {
		this.newId = newId;
	}
	
	private List<IdName> ids;

	public List<IdName> getIds() {
		return ids;
	}

	public void setIds(List<IdName> ids) {
		this.ids = ids;
	}



	public CorpCommpanyAction() {
		System.out.println("this is CorpCommpanyAction");
	}

	public String execute() throws Exception {
		System.out.println("this is execute");
		return null;
	}

	public String all() {
		HttpServletRequest request = ServletActionContext.getRequest();
		int start = Integer.valueOf(request.getParameter("start"));
		int limit = Integer.valueOf(request.getParameter("limit"));
		String sortname = request.getParameter("sortname");
		String sortorder = request.getParameter("sortorder");
		int currentPage = start;
		int pageSize = limit;
		HashMap<String, String> param = new HashMap<String, String>();
		if (sortname != null && sortname.length() > 0) {
			param.put("sortname", sortname);
		}
		if (sortorder != null && sortorder.length() > 0) {
			param.put("sortorder", sortorder);
		}
		rows = corpCommpanyMng.findByPage(currentPage, pageSize, param);
		this.limit = corpCommpanyMng.getCount(param);
		this.start = start;
		success = true;
		return SUCCESS;
	}

	public List<CorpCommpany> getRows() {
		return rows;
	}

	public void setRows(List<CorpCommpany> rows) {
		this.rows = rows;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public String newId(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String seqName = request.getParameter("seqName").trim();
		if(seqName==null || seqName.equals("")){
			newId=appSeqMng.getDefault();
		}else{
			newId = appSeqMng.getSequence(seqName);
		}
		success = true;
		return SUCCESS;
	}
	
	public String delete() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("delIds");
		ids = ids.substring(0, ids.length()-1);
		StringTokenizer st = new StringTokenizer(ids, ",", false);
		List<String> StringList = new ArrayList<String>();
		while (st.hasMoreElements()) {
			StringList.add(st.nextToken());
		}
		for (int i = 0; i < StringList.size(); i++) {
			corpCommpanyMng.remove(new Long(StringList.get(i)));
		}
		success = true;
		return SUCCESS;
	}

	public String add() {
		uploadFile();
		corpCommpany.setAddTime(new Date());
		corpCommpany.setUpdateTime(new Date());
		corpCommpany.setClickNum(0);
		corpCommpanyMng.add(corpCommpany);
		success = true;
		uploadFileResponse();
		return SUCCESS;
	}

	public String modify() {
		uploadFile();
		corpCommpany.setUpdateTime(new Date());
		corpCommpanyMng.update(corpCommpany);
		success = true;
		uploadFileResponse();
		return SUCCESS;
	}
	
	public String ids() {
		rows=null;
		ids = corpCommpanyMng.findIdNames();
		success = true;
		return SUCCESS;
	}

	
	
	private void uploadFile(){
		if(logo!=null){
			logoFileName=System.currentTimeMillis()%11973+(int)(Math.random()*1297)+logoFileName;
			uploadFile(logo,logoFileName);
			corpCommpany.setCorpLogo(logoFileName);
		}
	
	}

	
	private void uploadFile(File file, String fileName) {
		String  root = ServletActionContext.getServletContext().getRealPath("/upload/commpany");
		String path = root+"\\"+corpCommpany.getId();
		FileUploadUtil.uploadFile(file, fileName, path);
	}
	
	
	private void uploadFileResponse(){
		try {
			ServletActionContext.getResponse().getWriter().write("{success:true}");
			ServletActionContext.getResponse().setContentType("text/html");
			ServletActionContext.getResponse().getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	

	//
	private CorpCommpany corpCommpany;

	public CorpCommpany getCorpCommpany() {
		return corpCommpany;
	}

	public void setCorpCommpany(CorpCommpany corpCommpany) {
		this.corpCommpany = corpCommpany;
	}
	// logo
	private File logo;
	private String logoFileName;
	
	public File getLogo() {
		return logo;
	}

	public void setLogo(File logo) {
		this.logo = logo;
	}

	public String getLogoFileName() {
		return logoFileName;
	}

	public void setLogoFileName(String logoFileName) {
		this.logoFileName = logoFileName;
	}


}
